package com.manong.controller;

import java.util.Properties;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import com.manong.utils.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value="/api/email")
public class EmailController {
	@Value("${spring.mail.username}")
	private String sendName;
	@Resource
	private JavaMailSender javaMailSender;

    @GetMapping("/send")
    public Result sendMail03(){
            MimeMessage mMessage=javaMailSender.createMimeMessage();//创建邮件对象
            MimeMessageHelper mMessageHelper;
            Properties prop = new Properties();
            	 try {
	                	 mMessageHelper=new MimeMessageHelper(mMessage,true);
	                     mMessageHelper.setFrom(sendName);//发件人邮箱
	                     mMessageHelper.setTo("1501054562@qq.com");//收件人邮箱
	                     mMessageHelper.setSubject("光敏报警");//邮件的主题
	                     mMessageHelper.setText("光敏大于1000lx发出报警");
	                     javaMailSender.send(mMessage);//发送邮件
                 } catch (MessagingException e) {
                     e.printStackTrace();
					 return Result.error().message("邮件发送失败");
                 }
		return Result.ok().message("邮件发送成功");
	}	
}

